package com.numbfish.ch03_ex05_module_b;

import com.numbfish.ch03_ex04_module_a_stater.pojo.PersonFromModuleA;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;

@SpringBootTest
@Slf4j

public class Ch03Ex05ModuleBTestApp {
    @Autowired
    private  ApplicationContext applicationContext;




    @Test
    void contextLoads() {
        log.info("applicationContext = {}",applicationContext);
    }
    @Test
    void beanCount(){
        // 用户未定义任何类时的bean个数为 springBoot2.7 : 139
        // 用户未定义任何类时的bean个数为 springBoot3   : 152
        log.info("applicationContext default bean count= {}",applicationContext.getBeanDefinitionNames().length);
    }


    void outputTheBeanFromContext(Class<?> clazz){
        // 用户未定义任何类时的bean个数为152
        log.info("applicationContext get a bean  {} === {}",clazz,applicationContext.getBean(clazz));
    }

    // 使用注解方式也可以将依赖包中的Bean注入进来
    @Test
    public void test1(){
        String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames();
        Map<String, PersonFromModuleA> beansOfType = applicationContext.getBeansOfType(PersonFromModuleA.class);
        Collection<PersonFromModuleA> people = beansOfType.values();
        people.stream().forEach(System.out::println);
        Arrays.stream(beanDefinitionNames).filter(name->name.contains("person")).forEach(System.out::println);
    }
}